﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Xml.Serialization;

namespace Toybox.NicoSharp.Seiga
{
	using Extensions;
	using Utilities;

	public enum NgType
	{
		[XmlEnum("0")]
		Comment,

		[XmlEnum("1")]
		UserId,

		[XmlEnum("2")]
		Command,
	}

	[XmlRoot(@"response")]
	public class NgClients
	{

		#region [Static]

		public static string CommentNgUrl
		{
			get { return NicoUrls.SeigaApi + "comment/ng/"; }
		}


		public static NgClients Get(CookieCollection cookies = null)
		{
			string xml = NicoWebClient.Get(CommentNgUrl + "get", cookies.Coerce());
			return Deserializer.XmlDeserializer<NgClients>(xml);
		}

		public static NgResult Add(NgType type, string source, CookieCollection cookies = null)
		{
			return GetNgResult("add", type, source, cookies);
		}

		public static NgResult Delete(NgType type, string source, CookieCollection cookies = null)
		{
			return GetNgResult("delete", type, source, cookies);
		}

		protected static NgResult GetNgResult(string method, NgType type, string source, CookieCollection cookies = null)
		{
			cookies = cookies.Coerce();

			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("type", ((int)type).ToString());
			dict.Add("user_id", IdUtility.GetUserId(cookies));
			dict.Add("source", Uri.EscapeDataString(source));
			string data = Serializer.AndEqualPairSerializer(dict).Replace("_", "%5F");

			string xml = NicoWebClient.Post(CommentNgUrl + method, data, cookies);
			return Deserializer.XmlDeserializer<NgResult>(xml);
		}

		#endregion [Static]


		#region Public Members

		public int count;

		[XmlElement]
		public NgClient[] ngclient;

		#endregion Public Members


		#region [SubClass]

		public class NgClient
		{
			public string id;

			public int type;

			public string source;

			public string created;
		}

		[XmlRoot(@"response")]
		public class NgResult
		{
			public bool result;
		}

		#endregion [SubClass]

	}
}
